package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hmdp.utils.RedisConstants.FOLLOWS;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        //1.获取当前登录用户id
        Long userId = UserHolder.getUser().getId();
        //2.判断是关注还是取关
        String key = FOLLOWS + userId.toString();
        if (isFollow) {
            //2.1关注，新增数据,同时放入redis中的set集合,用于统计有谁关注了这个博主,博主id就是followUserId
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean isSuccess = save(follow);
            //如果关注成功，就放入redis集合
            if (isSuccess) {
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }

        } else {
            //2.2取关，删除
            boolean isSuccess = remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId)
                    .eq("follow_user_id", followUserId)
            );
            if (isSuccess) {
                stringRedisTemplate.opsForSet().remove(key);
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long followUserId) {
        //1.获取当前登录用户id
        Long userId = UserHolder.getUser().getId();
        //2.查询是否关注
        Integer count = query().eq("user_id", userId)
                .eq("follow_user_id", followUserId)
                .count();
        return Result.ok(count > 0);
    }

    @Override
    public Result followCommons(Long blogUpId) {
        //1.获取当前用户
        Long userId = UserHolder.getUser().getId();
        String key1 = FOLLOWS + userId.toString();
        //2.求交集,看当前登录用户和浏览的up主用户是否有共同关注
        //浏览的当前up主用户的id
        String key2 = FOLLOWS + blogUpId;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        //3.解析出id集合,该ids表示了关注了当前up主的用户id
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        if (intersect == null || intersect.isEmpty()) {
            return Result.fail("无共同关注");
        }
        //4.查询用户
        List<UserDTO> userDTOS = userService.listByIds(ids).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
